package day21.zhiyou100;

import java.net.DatagramPacket;
import java.net.DatagramSocket;


class ReceiveUDP2 {
    public static void main(String[] args) throws Exception {
        System.out.println("接收端已经启动，正在等待接收信息。。。。");
        byte[] bytes = new byte[1024];// 接收1kb
        DatagramSocket ds = new DatagramSocket(8088);
        while (true) {
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length);// 字节的数组  字节的长度
            ds.receive(dp);
            bytes = dp.getData();
            String receive = new String(bytes);
            System.out.println("接收的信息为：" + receive);
            bytes = new byte[1024];
            if (receive.contains("end")) {
                break;
            }
        }
        // 关闭
        ds.close();
    }
}

public class ReceiveUDP {
    public static void main(String[] args) throws Exception {
        // DatagramPacket
        // 构建一个字节数组容器
        System.out.println("接收端已经启动，正在等待接收信息。。。。");
        byte[] bytes = new byte[1024];// 接收1kb
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);// 字节的数组  字节的长度
        // DatagramSocket  接收服务
        DatagramSocket ds = new DatagramSocket(8088);
        ds.receive(dp);
        // 查看接收到的内容
        byte[] data = dp.getData();
        System.out.println("接收的内容为：" + new String(data));
        System.out.println("接收内容的长度为："+dp.getLength());
        // 关闭打开的系统资源
        ds.close();

    }
}
